﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using ZhongKeSite.Common;
using ZhongKeSite.Common.Helper;
using ZhongKeSite.Common.Utility;
using ZhongKeSite.Entity;
using ZhongKeSite.Web.Models;

namespace ZhongKeSite.Web.Areas.Admin.Controllers
{
    [Area("Admin")]
    [Route("Admin/[controller]/[action]")]
    public class FileController : BaseController
    {
        IPathProvider pathProvider;
        public FileController(IPathProvider _pathProvider)
        {
            pathProvider = _pathProvider;
        }
        /// <summary>
        /// 上传到本地文件夹
        /// </summary>
        /// <param name="formData"></param>
        /// <returns></returns>
        [RoleAuthorize("NewsAdd", "NewsEdit", "SinglePageAdd", "SinglePageEdit", "TemplateAdd", "TemplateEdit")]
        public IActionResult UpLoad([FromForm]IFormCollection formData)
        {
            var resultResponse = new ResultResponse<string>();
            try
            {
                //上传服务器
                var formFile = formData.Files[0];
                string relativePath = FileHelper.UploadFile(pathProvider, formFile);
                if (!string.IsNullOrWhiteSpace(relativePath))
                {
                    resultResponse.success = true;
                    resultResponse.msg = CommonConst.Success;
                    resultResponse.data = relativePath;
                }
                else
                {
                    resultResponse.msg = CommonConst.Fail;
                }
            }
            catch (Exception ex)
            {
                LogHelper.Error(ex);
                resultResponse.success = false;
                resultResponse.msg = CommonConst.Fail;
            }
            return Json(resultResponse);
        }
        /// <summary>
        /// 富文本编辑器上传文件
        /// </summary>
        /// <param name="formData"></param>
        /// <returns></returns>
        [RoleAuthorize("NewsAdd", "NewsEdit", "SinglePageAdd", "SinglePageEdit", "TemplateAdd", "TemplateEdit")]
        [HttpPost]
        public IActionResult UpLoadFile([FromForm]IFormCollection formData)
        {
            var resultResponse = new ResultResponse<string>();
            try
            {
                Dictionary<string, string> keyValuePairs = new Dictionary<string, string>();
                keyValuePairs.Add("UserId", CurrentUser.UserID);
                keyValuePairs.Add("Token", DESEncrypt.Encrypt(CurrentUser.UserID, DESEncrypt.Key));

                //上传服务器
                var result = HttpHelper.HttpUploadFile<ResultResponse<UploadFileInfo>>(string.Format("{0}/File/UploadFiles", AppSettings.FileDomain), System.Text.Encoding.UTF8, formData, keyValuePairs);
                LogHelper.Error("上传文件返回：" + result.success);
                if (result.success)
                {
                    resultResponse.success = true;
                    resultResponse.data = result.data.FilePath;
                }
                else
                {
                    resultResponse.msg = CommonConst.Fail;
                }
            }
            catch (Exception ex)
            {
                LogHelper.Error(ex);
                resultResponse.success = false;
                resultResponse.msg = CommonConst.Fail;
            }
            return Json(resultResponse);
        }

        /// <summary>
        /// 上传到本地文件夹
        /// </summary>
        /// <returns></returns>
        [RoleAuthorize("NewsAdd", "NewsEdit", "SinglePageAdd", "SinglePageEdit","TemplateAdd", "TemplateEdit")]
        public IActionResult UploadImage()
        {
            try
            {
                //上传服务器
                var formFile = Request.Form.Files[0];
                string relativePath = FileHelper.UploadFile(pathProvider, formFile);

                if (!string.IsNullOrWhiteSpace(relativePath))
                {
                    return Json(new { fileName = formFile.FileName, uploaded = 1, url = relativePath });
                }
            }
            catch (Exception ex)
            {
                LogHelper.Error(ex);
            }
            return Json(new { fileName = "上传失败", uploaded = 0 });
        }
    }
}